import allure, time
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
from base.base_common import BaseCommon
from base.base_wait import BaseWait


class PageCommonSteps(BaseAction):
    def __init__(self, driver):
        super().__init__(driver) # 继承BaseAction公用方法
        # 元素：导入/路径输入
        # self.file_path_input = By.XPATH, '//div[@class="formItem clearfix"]/div/div/input[@class="ivu-input ivu-input-default"]'
        self.file_path_input = By.XPATH, "//*[@placeholder='请选择文件']"
        # 元素：导入/选择文件
        self.file_choose_btn = By.XPATH, "//span[contains(text(),'选择文件')]"
        # 元素：勾选框
        self.table_select = By.XPATH,"//*[@class='ivu-table-cell ivu-table-cell-with-selection']"
        # 元素：勾选框
        text = "空"
        self.btn_radio = By.XPATH,"//*[text()='{0}']/span/span".format(text)
        # 元素：确定按钮
        self.confirm_btn = By.XPATH, '//div[@class="ivu-modal-wrap"]/div[@class="ivu-modal"]/div[@class="ivu-modal-content"]/div[@class="ivu-modal-footer"]/div/button[@class="ivu-btn ivu-btn-primary"]'
        self.confirm_btn_2 = By.XPATH, "//div[@class='ivu-modal-confirm-footer']/button[2]"

        # 元素：管理后台/导出提示
        self.alert_admin = By.CLASS_NAME, "ivu-message"
        # 元素：后台二次提示
        self.txt_tips = By.XPATH, "//p[@class='tips-box']"

        # 元素：/查询
        self.btn_search = By.XPATH, "//span[contains(text(),'查询')]"
        # 元素：/刷新
        self.btn_flush = By.XPATH, "//span[contains(text(),'刷新')]"
        # 元素：添加 按钮
        self.btn_add = By.XPATH,  "//span[contains(text(),'添加')]"
        # 元素：保存 按钮
        self.btn_save = By.XPATH, "//*[text()='保存']"
        # 元素：导出 按钮
        self.btn_export = By.XPATH, "//*[text()='导出']"
        # 元素：导入 按钮
        self.btn_import = By.XPATH, "//*[text()='导入']"
        # 元素：数据栏/编辑
        self.btn_data_to_edit = By.XPATH, "//tbody/tr[1]/td[1]/div[1]/div[1]/div[1]/*[contains(text(),'编辑')]"
        # 元素：数据栏/查看
        self.data_1st_view = By.XPATH,"//tbody/tr[1]/td[1]/div[1]/div[1]/div[1]/label[2]"
        # 元素：数据栏/删除
        self.data_1st_del = By.XPATH,"//tbody/tr[1]/td[1]/div[1]/div[1]/label[1]"
        # 元素：数据栏/全选勾选框
        self.data_all_choose = By.XPATH,"//thead/tr[1]/th[1]/div[1]/label[1]/span[1]"
        # 元素：数据栏/批量删除
        self.btn_all_delete = By.XPATH,"//span[contains(text(),'批量删除')]"
        # 元素：编辑 按钮
        self.btn_edit_2 = By.XPATH, "//span[contains(text(),'编辑')]"
        # 元素：删除 按钮
        self.btn_delete_2 = By.XPATH, "//span[contains(text(),'删除')]"
        # 元素：禁用 按钮
        self.btn_disable_2 = By.XPATH,  "//span[contains(text(),'禁用')]"

    @allure.step(title="操作步骤:给导入按钮定位和文件路径，导入文件")
    def click_import_file(self, locater_import, file_path):
        self.click(locater_import)  # 点击导入按钮
        self.click(self.file_path_input)  # 输入文件路径,选择文件
        self.click(self.file_choose_btn)
        # self.upload_file(filepath=file_path)  # 原生上传文件
        BaseCommon.upload_file(self, filepath=file_path)  # 原生上传文件
        self.click(self.confirm_btn)  # 弹窗，点击确认
        time.sleep(5)





    @allure.step(title="操作步骤:点击搜索")
    def click_to_search(self):
        time.sleep(2)
        return self.click(self.btn_search)

    @allure.step(title="操作步骤:点击刷新")
    def click_to_flush(self):
        return self.click(self.btn_flush)

    @allure.step(title="操作步骤:点击添加")
    def click_to_add(self):
        return self.click(self.btn_add)

    @allure.step(title="操作步骤:点击保存")
    def click_to_save(self):
        self.click(self.btn_save)
        # return self.click(self.btn_save)

    @allure.step(title="操作步骤:点击导出")
    def click_to_export(self):
        return self.click(self.btn_export)

    @allure.step(title="操作步骤:选择第几个数据点击指定操作按钮")
    def choose_data_to_click(self,locater):
        return self.click(locater)

    @allure.step(title="操作步骤:列表数据，点击编辑按钮")
    def click_to_data_edit(self):
        return self.click(self.btn_data_to_edit)
        # return self.find_els(self.btn_data_to_edit)[index].click()

    @allure.step(title="操作步骤:搜索后的某条数据，点击编辑按钮")
    def search_and_click_to_edit(self):
        return self.click(self.btn_edit_2)

    @allure.step(title="操作步骤:搜索后的某条数据，点击删除按钮")
    def search_and_click_to_delete(self):
        self.click(self.btn_delete_2)
        return self.alert_click_to_confirm()

    @allure.step(title="操作步骤:搜索后的某条数据，点击禁用按钮")
    def search_and_click_to_disable(self):
        self.click(self.btn_disable_2)
        return self.alert_click_to_confirm()

    @allure.step(title="操作步骤:单选radio")
    def click_give_text_radio(self,text,reason=0):
        self.btn_radio = By.XPATH,"//label[text()='{0}']".format(text)
        self.find_els(self.btn_radio)[reason].click() # 点击单选

    @allure.step(title="操作步骤:勾选checkbox")
    def click_give_order_checkbox(self,order=0):
        # self.btn_checkbox = By.XPATH,"//*[@type='checkbox' and @class='ivu-checkbox-input']"
        self.btn_checkbox = By.XPATH,"//*[@type='checkbox']/parent::*/parent::*"
        self.find_els(self.btn_checkbox)[order].click() # 点击勾选指定勾选框
        return time.sleep(1)

    @allure.step(title="操作步骤:输入关键字并选择")
    def click_and_input_keys(self, locate, keys):
        self.clear(locate)
        time.sleep(0.5)
        return self.input(locate, keys)

    @allure.step(title="操作步骤:模糊匹配，输入检索并选择")
    def click_and_input_keys_to_select(self, locate, keys):
        self.input(locate, keys)
        time.sleep(1)
        # 元素：查询条件/下拉选项第一各
        # to_select = By.XPATH, "//li[text()='{0}']".format(keys)
        to_select = By.XPATH, "//li[contains(text(),'{0}')]".format(keys)
        return self.click(to_select)


    @allure.step(title="操作步骤:精准匹配，输入检索并选择")
    def click_and_input_keys_to_select_1(self, locate, keys):
        self.input(locate, keys)
        time.sleep(1)
        # 元素：查询条件/下拉选项第一各
        to_select = By.XPATH, "//li[text()='{0}']".format(keys)
        return self.click(to_select)

    @allure.step(title="操作步骤:点击弹出选项，选择")
    def click_and_input_keys_to_select_2(self, locate, keys):
        self.click(locate)
        to_select = By.XPATH, "//li[text()='{0}']".format(keys)
        return self.click(to_select)  # 编辑

    @allure.step(title="操作步骤:多个相同的处理，点击弹出选项，选择")
    def click_and_input_keys_to_select_3(self, locate,order,keys):
        self.click(locate)
        to_select = By.XPATH, "//li[text()='{0}']".format(keys)
        return self.find_els(to_select)[order].click()

    @allure.step(title="操作步骤:点击归属部门并选择")
    def click_and_input_bm_to_select(self,locate,bm1,bm2):
        # self.click(locate)
        # a = "三学 / 销售部"
        # ownership = bm1+" / "+bm2
        # self.inputs(locate,ownership )
        # self.click(locate)
        # # 元素：管理后台/查询条件/下拉选项第一各
        # to_select1 = By.XPATH, "//span[contains(text(),'{0}')]".format(ownership)
        # self.click(to_select1)
        to_select1 = By.XPATH, "//ul[@class='ivu-cascader-menu']/li[contains(text(),'{0}')]".format(bm1)
        to_select2 = By.XPATH, "//ul[@class='ivu-cascader-menu']/li[contains(text(),'{0}')]".format(bm2)
        self.click(locate)
        self.click(to_select1)
        self.click(to_select2)
        return time.sleep(1)

    @allure.step(title="操作步骤:修改选项，点击输入检索条件并选择第一个")
    def input_keys_choose_to_edit(self, locate, keys):
        """
        :rtype: BaseAction
        """
        self.click(locate)
        BaseCommon.press_back(self, num=8)
        self.input(locate, keys)
        # 元素：管理后台/查询条件/下拉选项第一各
        BaseCommon.choose_1st_select(self)
        # to_select = By.XPATH, "//li[contains(text(),'{0}')]".format(keys)
        # return self.click(to_select)

    @allure.step(title="操作步骤:选中指定行数，选中数据")
    def data_to_select_and_click(self,inde):
        """
        :rtype: object
        """
        self.find_els(self.table_select)[inde].click()  # 选中数据

    @allure.step(title="操作步骤:编辑页面点击确认")
    def click_to_confirm(self):
        """
        :rtype: object
        """
        self.click(self.confirm_btn)  # 弹窗，点击确认

    @allure.step(title="操作步骤:alert弹窗点击确认")
    def alert_click_to_confirm(self):
        """
        :rtype: object
        """
        self.click(self.confirm_btn_2)  # 弹窗，点击确认

    @allure.step(title="操作步骤:判断某个元素是否可见")
    def visibility_of_element_located(self, locator,el_index):
        """
        :rtype: object
        """
        # 判断某个元素是否可见. 可见代表元素非隐藏，并且元素的宽和高都不等于0，符合返回True，否则返回False
        return  self.eleIsNotExist(locator,el_index)

    @allure.step(title="操作步骤:返回管理后台是否有提示，提示文本")
    def return_admin_notice_text(self,num,el_index=1):
        """
        :rtype: object
        """
        alert_admin ,txt_tips= self.alert_admin ,self.txt_tips
        locator_visible ,r_status= False ,False
        text = ""
        for i in range(num):
            r_status = self.visibility_of_element_located(alert_admin,el_index)
            if r_status:
                # text = BaseAction.getTextContents(self, feature=alert_admin)  # 获取后台返回提示
                text = BaseAction.getTextContent(self, feature=alert_admin)  # 获取后台返回提示
                r_status = True
                break
            continue
        # print(str(text))
        return r_status,text